package com.cashmate.struts;

import com.cashmate.hibernate.dao.AreaDAO;
import com.cashmate.hibernate.dao.BranchDAO;
import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.pojo.Areas;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class EditAreas extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditAreas.class);
    private String oper;
    private String id;
    private String areaname;
    private String areacode;
    private String collector;
    private String branch;
    private final AreaDAO areas = (AreaDAO) ServletActionContext.getServletContext().getAttribute("areaDAO");
    private final EmployeeDAO empDao = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private final BranchDAO brDao = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    @Action(value = "/edit-areas", results = {
        @Result(name = "duplicate", location = "../error/formError.jsp"),
        @Result(name = "error", location = "../error/messages.jsp")
    })
    @Override
    public String execute() {
        try {
            Areas a = null;
            if (oper.equals("add")) {
                if (areas.hasDuplicate(areacode)) {
                    addActionError("ERROR : Area code" + areacode + " already exists.");
                    return "duplicate";
                }
                a = new Areas(areaname, areacode, true);
                if (!collector.equals("-1")) {
                    a.setEmployees(empDao.get(Integer.parseInt(collector)));
                }
                if (!branch.equals("-1")) {
                    a.setBranches(brDao.get(Integer.parseInt(branch)));
                }
                areas.insert(a);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") added a new Area: {areaid:" + id + ", areaname:" + areaname + ", areacode:" + areacode + ", collector:" + a.getCollector() + ", branch:" + a.getBranch() + "}");
            } else if (oper.equals("edit")) {
                a = areas.get(Integer.parseInt(id));

                if (!a.getAreacode().equals(areacode)) {
                    if (areas.hasDuplicate(areacode)) {
                        addActionError("ERROR : Area code" + areacode + " already exists.");
                        return "duplicate";
                    }
                }
                String old = a.getAreacode();
                a.setAreacode(areacode);
                a.setAreaname(areaname);
                if (!collector.equals("-1")) {
                    a.setEmployees(empDao.get(Integer.parseInt(collector)));
                }
                if (!branch.equals("-1")) {
                    a.setBranches(brDao.get(Integer.parseInt(branch)));
                }
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Area(" + old + ") with new values: {areaid:" + id + ", areaname:" + areaname + ", areacode:" + areacode + ", collector:" + a.getCollector() + ", branch:" + a.getBranch() + "}");
            } else if (oper.equals("del")) {
                a = areas.get(Integer.parseInt(id));
                a.setIsactive(false);
                areas.update(a);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") deleted Area(" + a.getAreacode() + ")");
            }
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }

    public void setAreacode(String areacode) {
        this.areacode = areacode;
    }

    public void setAreaname(String areaname) {
        this.areaname = areaname;
    }

    public void setBranch(String branch) {
        this.branch = branch;
    }

    public void setCollector(String collector) {
        this.collector = collector;
    }

    public void setId(String id) {
        this.id = id;
    }

    public static void setLogger(Logger logger) {
        EditAreas.logger = logger;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }
}
